package l1j.server.server.model;

import static l1j.server.server.model.skill.L1SkillId.ERASE_MAGIC;

import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.ActionCodes;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_EffectLocation;

public class L1Chaserl extends TimerTask {
	private static Logger _log = Logger.getLogger(L1Chaserl.class.getName());

	private ScheduledFuture<?> _future = null;
	private int _timeCounter = 0;
	private final L1PcInstance _pc;
	private final L1Character _cha;
	private final int _attr;
	private final int _gfxid;

	public L1Chaserl(final L1PcInstance pc, final L1Character cha, final int attr, final int gfxid) {
		this._cha = cha;
		this._pc = pc;
		this._attr = attr;
		this._gfxid = gfxid;
	}

	public void attack() {
		double damage = this.getDamage(this._pc, this._cha);
		if (this._cha.getCurrentHp() - (int) damage <= 0 && this._cha.getCurrentHp() != 1) {
			damage = this._cha.getCurrentHp() - 1;
		} else if (this._cha.getCurrentHp() == 1) {
			damage = 0;
		}

		this._pc.sendPacketsX8(new S_EffectLocation(this._cha.getX(),this._cha.getY(), this._gfxid));
		if (this._cha instanceof L1PcInstance) {
			final L1PcInstance pc = (L1PcInstance) this._cha;
			pc.sendPacketsX8(new S_DoActionGFX(pc.getId(),ActionCodes.ACTION_Damage));
			pc.receiveDamage(this._pc, damage, false);
		} else if (this._cha instanceof L1NpcInstance) {
			final L1NpcInstance npc = (L1NpcInstance) this._cha;
			npc.broadcastPacketX8(new S_DoActionGFX(npc.getId(),ActionCodes.ACTION_Damage));
			npc.receiveDamage(this._pc, (int) damage);
		}
	}

	public void begin() {
		//TODO 魔法出現後延遲時間判斷
		this._future = GeneralThreadPool.getInstance().scheduleAtFixedRate(this, 0,1200);
	}
	//TODO 魔法出現後延遲時間判斷

	public int get_attr() {
		return this._attr;
	}

	public double getDamage(final L1PcInstance pc, final L1Character cha) {
		double dmg = 0;
		final L1Magic _magic = new L1Magic(pc, cha);
		if (this._gfxid == 6509) {
			dmg = _magic.calcMagicDamage(10148);
		} else if (this._gfxid == 6509) {
			dmg = _magic.calcMagicDamage(10149);
		} else if (this._gfxid == 6509) {
			dmg = _magic.calcMagicDamage(10150);
		} else if (this._gfxid == 6509) {
			dmg = _magic.calcMagicDamage(10151);
		} else if (this._gfxid == 6512) {
			dmg = _magic.calcMagicDamage(10152);
		}
		cha.removeSkillEffect(ERASE_MAGIC);

		return dmg;
	}

	@Override
	public void run() {
		try {
			if (this._cha == null || this._cha.isDead()) {
				this.stop();
				return;
			}
			this.attack();

			//TODO 時間計時器用來判斷魔法出現次數
			this._timeCounter++;
			if (this._timeCounter >= 2) {
				this.stop();
				return;
			}
			//TODO 時間計時器用來判斷魔法出現次數

		} catch (final Throwable e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
	}

	public void stop() {
		if (this._future != null) {
			this._future.cancel(false);
		}
	}
}
